package com.longxi.tianjintianbao.ui.activity.main;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Button;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.presenter.main.SplashAtPresenter;
import com.longxi.tianjintianbao.ui.view.main.ISplashAtView;
import com.longxi.tianjintianbao.utils.LogUtils;

import java.util.HashSet;
import java.util.Set;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;


/**
 * Created by ZhaoWei on 2016/5/6.
 * 欢迎页
 */
public class SplashActivity extends BaseActivity<ISplashAtView, SplashAtPresenter> implements ISplashAtView {

    @BindView(R.id.btnJump)
    Button mBtnJump;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initData() {
        super.initData();

//        algorithm();

        if("".equals(AppConst.identification)){
            showWaitingDialog("初始化中");
            registerMessageReceiver();
        }else{
            mPresenter.getAppInitInfo();
        }
    }

    public void algorithm(){
        lengthOfLongestSubstring("abcacba");
    }

    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        Set<Character> set = new HashSet<>();
        int ans = 0, i = 0, j = 0;
        LogUtils.d("algorithm", "j=" + j + ",i=" + i + ",ans=" + ans + ",set=" + set.toString());
        while (i < n && j < n) {
            // try to extend the range [i, j]
            if (!set.contains(s.charAt(j))){
                set.add(s.charAt(j++));
                ans = Math.max(ans, j - i);
            }
            else {
                set.remove(s.charAt(i++));
            }

            LogUtils.d("algorithm", "j=" + j + ",i=" + i + ",ans=" + ans + ",set=" + set.toString());
        }
        return ans;
    }

    @Override
    public void initListener() {
        super.initListener();
        mBtnJump.setOnClickListener(v -> mPresenter.getHome());
    }

    @Override
    protected SplashAtPresenter createPresenter() {
        return new SplashAtPresenter(this);
    }

    @Override
    public Button getBtnJump() {
        return mBtnJump;
    }

    @Override
    protected void onDestroy() {
        mPresenter.timerCancel();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.onActivityResult(requestCode, resultCode, data);
    }

    //for receive customer msg from jpush server
    private MessageReceiver mMessageReceiver;

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(AppConst.MESSAGE_RECEIVED_ACTION);
        filter.addAction(AppConst.MESSAGE_REGISTRATION_ID);
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (AppConst.MESSAGE_REGISTRATION_ID.equals(intent.getAction())){
                    AppConst.identification = JPushInterface.getRegistrationID(MyApplication.getInstance());
                    hideWaitingDialog();
                    mPresenter.getAppInitInfo();
                }
            } catch (Exception e){
            }
        }
    }

}
